package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpDuizhaoMapper;
import com.ruoyi.system.domain.SpDuizhao;
import com.ruoyi.system.service.ISpDuizhaoService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 测评对照Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class SpDuizhaoServiceImpl implements ISpDuizhaoService 
{
    @Autowired
    private SpDuizhaoMapper spDuizhaoMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询测评对照
     * 
     * @param dzId 测评对照主键
     * @return 测评对照
     */
    @Override
    public SpDuizhao selectSpDuizhaoByDzId(Long dzId)
    {
        return spDuizhaoMapper.selectSpDuizhaoByDzId(dzId);
    }

    /**
     * 查询测评对照列表
     * 
     * @param spDuizhao 测评对照
     * @return 测评对照
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpDuizhao> selectSpDuizhaoList(SpDuizhao spDuizhao)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spDuizhao.setDatascope(datascope);
        if(datascope==4){
            spDuizhao.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spDuizhao.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spDuizhao.setCheckHuiyuanId(0l);
            }

        }
        return spDuizhaoMapper.selectSpDuizhaoList(spDuizhao);
    }

    @Override
    public List<SpDuizhao> selectSpDuizhaoListWeb(SpDuizhao spDuizhao)
    {
        return spDuizhaoMapper.selectSpDuizhaoList(spDuizhao);
    }

    @Override
    public int importUser(List<SpDuizhao> list) {
        for(SpDuizhao dz:list){
            spDuizhaoMapper.insertSpDuizhao(dz);
        }
        return 0;
    }

    /**
     * 新增测评对照
     * 
     * @param spDuizhao 测评对照
     * @return 结果
     */
    @Override
    public int insertSpDuizhao(SpDuizhao spDuizhao)
    {
        return spDuizhaoMapper.insertSpDuizhao(spDuizhao);
    }

    /**
     * 修改测评对照
     * 
     * @param spDuizhao 测评对照
     * @return 结果
     */
    @Override
    public int updateSpDuizhao(SpDuizhao spDuizhao)
    {
        return spDuizhaoMapper.updateSpDuizhao(spDuizhao);
    }

    /**
     * 批量删除测评对照
     * 
     * @param dzIds 需要删除的测评对照主键
     * @return 结果
     */
    @Override
    public int deleteSpDuizhaoByDzIds(Long[] dzIds)
    {
        return spDuizhaoMapper.deleteSpDuizhaoByDzIds(dzIds);
    }

    /**
     * 删除测评对照信息
     * 
     * @param dzId 测评对照主键
     * @return 结果
     */
    @Override
    public int deleteSpDuizhaoByDzId(Long dzId)
    {
        return spDuizhaoMapper.deleteSpDuizhaoByDzId(dzId);
    }
}
